#include"../include/p.h"
int s_Download(int newfd,char* filename)
{
	char buf[100]={0};
	//接收文件大小
	int filesize=0;
	recv(newfd,buf,sizeof(buf),0);
	//	recv(newfd,buf,sizeof(buf),0);
	printf("%s\n",buf);
	filesize=atoi(buf);
	printf("文件大小为%d\n",filesize);
	if(filesize==0)
	{
		printf("文件为空！\n");
		//	return -1;
	}
	memset(buf,'\0',sizeof(buf));
	//发送ok 表示收到文件大小
	send(newfd,"ok",sizeof("ok"),0);
	//接收文件内容
	int fw=open(filename,O_WRONLY|O_CREAT|O_TRUNC,0664);

	if(fw<0)
	{
		perror("open error \n");
		return -1;
	}
	int count=0;
	while(count<filesize)
	{
		memset(buf,'\0',sizeof(buf));
		int n=recv(newfd,buf,sizeof(buf),0);
		write(fw,buf,n);
		count+=n;
	}
	printf("文件下载成功！\n");
	close(fw);
	return 0;

}
